package com.drug.mingkang.controller;

import com.drug.mingkang.model.HqPurchasedetails;
import com.drug.mingkang.model.vo.PurchasedetailsVo;
import com.drug.mingkang.service.HqPurchasedetailsService;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 采购明细(HqPurchasedetails)表控制层
 *
 * @author wenchangjian
 * @since 2020-08-11 12:00:13
 */
@RestController
@RequestMapping("hqPurchasedetails")
@Api(value = "采购明细控制层", tags = "采购明细Contorller")
public class HqPurchasedetailsController {
    /**
     * 服务对象
     */
    @Resource
    private HqPurchasedetailsService hqPurchasedetailsService;
    @Resource
    private Tools tools;

    /**
     * 根据采购单id 查询采购明细
     * @param vo 参数列表
     * @return 采购明细列表
     */
    @ApiOperation(value = "根据采购单id 查询采购明细", notes = "test|page:1,limit:10", httpMethod = "POST", response = Map.class)
    @PostMapping("showAll")
    public Map<String, Object> showAll(PurchasedetailsVo vo) {
        List<HqPurchasedetails> list = hqPurchasedetailsService.queryAllByLimit(vo);
        int count = hqPurchasedetailsService.selectCount(vo);
        return tools.limit(list, count);
    }

    /**
     * 原材料明细增加
     * @param arr 原材料集合json
     * @param purchaseId 原材料采购id
     * @return 状态
     */
    @ApiOperation(value = "原材料明细增加", notes = "参数不能为空", httpMethod = "POST", response = ResponseMessage.class)
    @PostMapping("insert")
    public ResponseMessage insert(@ApiParam(name = "arr", value = "原材料集合json", required = true) String arr, @ApiParam(name = "purchaseId", value = "原材料采购id", required = true) String purchaseId) {
        boolean b = hqPurchasedetailsService.insertMaterial(arr, purchaseId);
        return b ? ResponseMessage.success() : ResponseMessage.error();
    }

    /**
     * 原材料明细删除
     * @param materialId 原材料id
     * @param purchaseId 原材料采购单id
     * @return 状态
     */
    @ApiOperation(value = "原材料明细删除", notes = "根据采购单id删除指定的原材料id数据", httpMethod = "POST", response = ResponseMessage.class)
    @PostMapping("delete")
    public ResponseMessage delete(@ApiParam(name = "materialId", value = "原材料id", required = true) String materialId, @ApiParam(name = "purchaseId", value = "原材料采购单id", required = true) String purchaseId) {
        boolean b = hqPurchasedetailsService.deleteMaterial(materialId, purchaseId);
        return b ? ResponseMessage.success() : ResponseMessage.error();
    }

    /**
     * 根据采购单id修改指定的原材料数量
     * @param materialId 原材料id
     * @param purchaseId 原材料采购单id
     * @param count 原材料数量
     * @return 状态
     */
    @ApiOperation(value = "根据采购单id修改指定的原材料数量", notes = "根据采购单id 修改指定的原材料数量", httpMethod = "POST", response = ResponseMessage.class)
    @PostMapping("update")
    public ResponseMessage update(@ApiParam(name = "materialId", value = "原材料id", required = true) String materialId, @ApiParam(name = "purchaseId", value = "原材料采购单id", required = true) String purchaseId, @ApiParam(name = "count", value = "原材料数量", required = true) int count) {
        boolean b = hqPurchasedetailsService.updateMaterial(materialId, purchaseId, count);
        return b ? ResponseMessage.success() : ResponseMessage.error();
    }

}